home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / manual-p / vh-man2h.000 / vh-man2h / vh-man2html-1.3 / manwhatis < prev    next >
Text File  |  1996-05-03  |  5KB  |  158 lines

  1. #!/usr/bin/awk -f
  2. #
  3. #  Generate a whatis index into the manual pages by using find tp 
  4. #  locate all the whatis files.
  5. #  Michael Hamilton <michael@actrix.gen.nz>
  6. #
  7. BEGIN {
  8.  
  9.   OFS="";
  10.   section = ARGV[1];
  11.   "echo $PPID" | getline pid;
  12.  
  13.   if (section !~ /^[0-9]$/) {
  14.     print "Content-type: text/html\n\n";  
  15.     print "<head>";
  16.     print "<title>Manual - Illegal section</title>";
  17.     print "</head>\n<body>";
  18.     print "Illegal section number '" section "'." ;
  19.     print "Must be 0..9 or all";
  20.     print "</body>";
  21.     exit;
  22.   }
  23.     
  24.   cache_dir  = "/var/man2html";
  25.   cache_file = "whatis-" section ".html";
  26.   cache = cache_dir "/" cache_file;
  27.                 # Find out the man path
  28.   "/usr/bin/man -w" | getline man_path
  29.   gsub(":", " ", man_path);
  30.                 # See if anything is out of date.
  31.   if (system("test -f " cache) == 0) {
  32.     cmd = "/usr/bin/find " man_path " -maxdepth 1 -name whatis -newer " cache;
  33.     cmd | getline need_update;
  34.   }
  35.   else {
  36.     need_update = 1;
  37.   }
  38.  
  39.   if (need_update != "") {
  40.  
  41.     cache_tmp  = cache "_" pid;
  42.     sort_tmp   = cache_dir "/manwhatis_tmp_" pid ;
  43.     buffer_tmp = cache_dir "/manwhatis_tmp2_" pid;
  44.  
  45.     sec_name[1] = "User Commands";
  46.     sec_name[2] = "System Calls";
  47.     sec_name[3] = "Library Functions";
  48.     sec_name[4] = "Special Files";
  49.     sec_name[5] = "File Formats";
  50.     sec_name[6] = "Games";
  51.     sec_name[7] = "Miscellany";
  52.     sec_name[8] = "Administration and Privileged Commands";
  53.     
  54.                 # Print heading
  55.     print "Content-type: text/html\n\n" > cache_tmp;  
  56.     print "<html>\n<head>" > cache_tmp;
  57.     print "<title>Manual Pages - Names and Descriptions: " section ". " sec_name[section] "</title>" > cache_tmp;
  58.  
  59.     print "</head>\n<body>" > cache_tmp;
  60.     print "<h1>Manual Pages - Names and Descriptions</h1>" > cache_tmp;
  61.     print "<h1>Section " section ": " sec_name[section] "</h1>" > cache_tmp;
  62.     "hostname" | getline hostname;
  63.     "date" | getline date;
  64.     print hostname " (" date ")" > cache_tmp;
  65.                 # Find out the man path 
  66.     "/usr/bin/man -w" | getline;
  67.     $1 = $1 ":";
  68.     gsub(":", " ", $1); 
  69.  
  70.     find_cmd = "/usr/bin/find " man_path " -maxdepth 1 -name whatis -printf '%p '";
  71.     find_cmd | getline whatis_files;
  72.     close(find_cmd);
  73.                 # Try to parse valid entries - those that contain ([0-9])
  74.     extract_cmd =  "/usr/bin/egrep -h '\\(" section "[A-Za-z]*\\)' " whatis_files ;
  75.  
  76.     print "<br>Manual pages referenced in " whatis_files "<p>" > cache_tmp;
  77.  
  78.     sort_cmd = "/usr/bin/sort -f > " sort_tmp;
  79.     while ( extract_cmd | getline ) { 
  80.       if (bracket_pos = index($0, "(")) {
  81.     sec_full_num = substr($0, bracket_pos + 1, index($0, ")") - bracket_pos - 1); 
  82.     names = substr($0, 1, bracket_pos - 2);
  83.                 # Get rid of blanks and commas.
  84.     n = split(names, name_list, " *, *");
  85.     description = substr($0, bracket_pos + length(sec_full_num) + 2);
  86.                 # Generate a entry for each name
  87.     for (i = 1; i <= n; i++) {
  88.       print name_list[i] " " sec_full_num " " name_list[1] " / " description | sort_cmd;
  89.     }
  90.       }
  91.     }
  92.     close(extract_cmd);
  93.     close(sort_cmd);
  94.     while (getline < sort_tmp) { 
  95.       letter = tolower(substr($1,1,1));
  96.       if (letter != last_letter) { 
  97.     if (last_letter) {
  98.       print "</dl><p>" > buffer_tmp;
  99.     }
  100.     last_letter = letter;
  101.     letter_index[++num_letters] = letter;
  102.                 # Terminate list, start a new one
  103.  
  104.     print "<h2> <a name=\"", letter, "\">", toupper(letter), "</a></h2>\n<dl>" > buffer_tmp ;
  105.       }
  106.                 # Generate a <dt> for the name
  107.       if ($3 != last_file || $1 != last_name) {    # Don't repeat the same entry link.
  108.     print "<dt><a href=\"http:/cgi-bin/man2html?", $3, "+", $2, "\">", $1, "(", $2, ")", "</a>" > buffer_tmp;
  109.     last_file = $3;
  110.     last_name = $1;
  111.       }
  112.       print "<dd>", substr($0, match($0, "/") + 1) > buffer_tmp;
  113.     }
  114.                 # Finish off last list
  115.     print "\n</dl><p>" > buffer_tmp;
  116.     close(buffer_tmp);
  117.  
  118.     system("rm " sort_tmp);
  119.  
  120.                 # Print out alphabetic quick index and other links
  121.     for (i = 1; i <= num_letters; i++) {
  122.       print "<a href=\"#" letter_index[i] "\">" toupper(letter_index[i]) "</a>" > cache_tmp;
  123.     }
  124.     print "<hr>" > cache_tmp;
  125.     print "<a href=\"http:/cgi-bin/man2html\">Return to Main Contents</a>" > cache_tmp;
  126.     
  127.     print "<p>Other sections:" > cache_tmp;
  128.     for (i=1; i<=8; i++) { 
  129.       if (i != section) {    # Dont print an entry for the section we are in
  130.     print "<a href=\"http:/cgi-bin/manwhatis?" i "\">" i ". " sec_name[i] "</a> " > cache_tmp;
  131.       }
  132.     }
  133.     print "<hr><p>" > cache_tmp;
  134.                 # Print out the accumulated contents entries
  135.     while ((getline < buffer_tmp)) print > cache_tmp;
  136.     print "<hr><p>" > cache_tmp;
  137.  
  138.     for (i = 1; i <= num_letters; i++) {
  139.       print "<a href=\"#" letter_index[i] "\">" toupper(letter_index[i]) "</a>" > cache_tmp;
  140.     }
  141.     print "<hr>" > cache_tmp;
  142.     print "<p><a href=\"http:/cgi-bin/man2html\">Return to Main Contents</a>" > cache_tmp;
  143.     
  144.     print "<p>Other sections:" > cache_tmp;
  145.     for (i=1; i<=8; i++) { 
  146.       if (i != section) {    # Dont print an entry for the section we are in
  147.     print "<a href=\"http:/cgi-bin/manwhatis?" i "\">" i ". " sec_name[i] "</a> " > cache_tmp;
  148.       }
  149.     }
  150.     print "</body>" > cache_tmp;
  151.     print "</html>" > cache_tmp;
  152.     system("/bin/mv " cache_tmp " " cache);
  153.     system("/bin/rm " buffer_tmp);
  154.   }
  155.   system("/bin/cat " cache);
  156.   exit;
  157. }
  158.